<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .container {
        width: 600px;
        height: 600px;
        border: 1px solid;
        margin: 0 auto;
        position: relative;
      }
    </style>
  </head>
  <body>
    <div class="container"></div>
    <script>
      //利用方块画身体
      function Rect(x, y, color) {
        this.x = x;
        this.y = y;
        this.color = color;
        this.createRect();
      }
      Rect.prototype.createRect = function () {
        this.rectEle = document.createElement("div");
        this.rectEle.style.width = "50px";
        this.rectEle.style.height = "50px";
        this.rectEle.style.position = "absolute";
        this.rectEle.style.left = this.x + "px";
        this.rectEle.style.top = this.y + "px";
        this.rectEle.style.background = this.color;
        this.rectEle.style.border = "1px solid";
      };
      //   let rect1 = new Rect(0, 0, "blue");
      //   let rect2 = new Rect(50, 0, "blue");
      //   console.log(rect1);
      //   let containerEle = document.querySelector(".container");
      //   containerEle.appendChild(rect1.rectEle);
      //   containerEle.appendChild(rect2.rectEle);
      function Snake(containerEle) {
        this.containerEle = containerEle;
        this.body = [];
        this.createBody();
        console.log(this.body);
        this.renderDom();
      }
      Snake.prototype.createBody = function () {
        for (let i = 0; i < 5; i++) {
          let rect;
          if (i == 4) {
            rect = new Rect(i * 50, 0, "pink");
          } else {
            rect = new Rect(i * 50, 0, "blue");
          }

          this.body.push(rect);
        }
      };
      Snake.prototype.renderDom = function () {
        this.body.forEach((rect) => {
          this.containerEle.appendChild(rect.rectEle);
        });
      };
      let containerEle = document.querySelector(".container");
      let snake = new Snake(containerEle);
    </script>
  </body>
</html>
